#### Generate Figure: Education by Agreement ####

#### Descriptives ####
fig6_dat = bind_rows(import(here("data","fig6_edu_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig6_edu_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |>
  invert_group() |> fix_healthcare()

## N significant
ccat("Number of (in)significant differences (education groups):")
fig6_dat |> 
  group_by(is_sig_int) |> 
  count() |> 
  mutate(n = n/2) # Account for double-counting of policies

## Average difference
ccat("Average significant difference:")
fig6_dat |> 
  filter(is_sig_int == 1) |> 
  pivot_wider(id_cols = c("condition","agree"),
              names_from = group, values_from = effect,
              names_prefix = "group_") |> 
  mutate(abs_diff = abs(group_0 - group_1)) |> 
  summarise(avg_abs_diff = mean(abs_diff))

## Correlation
ccat("High/low income correlation:")
fig6_dat |> 
  pivot_wider(id_cols = c("condition","agree"),
              names_from = group, values_from = effect,
              names_prefix = "group_") |> 
  summarise(cor = cor(group_0, group_1))

# Plot
fig_edu_agree = plot_conjoint(conjoint = fig6_dat |> 
                             filter(agree == "Agree"),
                           is_result = T,
                           lab = policies, 
                           weights = dta_grp$demog_weight[!is.na(dta_grp$edu_grp)],
                           use="for",
                           color_set=c("1"="grey","2"="black","3"="lightgreen","4"="darkgreen"),
                           title=NULL,
                           grouped_data=T) +
  labs(subtitle = "Agree with Policy", x = NULL, y = NULL)

fig_edu_disagree = plot_conjoint(conjoint = fig6_dat |> 
                                filter(agree == "Disagree"),
                              is_result = T,
                              lab = policies, 
                              weights = dta_grp$demog_weight[!is.na(dta_grp$edu_grp)],
                              use="against",
                              color_set=c("1"="grey","2"="black","3"="lightgreen","4"="darkgreen"),
                              title=NULL,
                              grouped_data=T) +
  labs(subtitle = "Disagree with Policy", x = NULL, y = NULL)

fig_edu_pts = fig_edu_agree + fig_edu_disagree

print(fig_edu_pts)

ggsave(here("figures","figure_6.png"), fig_edu_pts,
       dpi = 600, units = "in", width = 10, height = 9)

ccat("See 'figure_6.png' for Figure 6 output")


